﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Runtime.InteropServices;

namespace pcalc
{
	class RichConsole
	{
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr window, int message, int wparam, int lparam);

        const int WM_VSCROLL = 0x115;
        const int SB_BOTTOM = 7;

		RichTextBox _richTextBox;
		public Font Font { get; set; }
		public Color Color { get; set; }
		public bool Bullet { get; set; }
		public FontStyle FontStyle { get; set; }

		public RichConsole(RichTextBox rb)
		{
			_richTextBox = rb;
			rb.Multiline = true;
			rb.ScrollBars = RichTextBoxScrollBars.Both;
			Font = new Font("Courier New", 9, FontStyle.Regular);
			rb.Font = Font;
			Color = Color.Black;
			Bullet = false;
		}

		public void Append(string str)
		{
			_richTextBox.Select(_richTextBox.Text.Length, 0);
			_richTextBox.SelectionFont = new Font(Font.FontFamily, Font.Size, FontStyle);
			_richTextBox.SelectionColor = Color;
			_richTextBox.SelectionBullet = Bullet;
			_richTextBox.SelectedText = str;
            SendMessage(_richTextBox.Handle, WM_VSCROLL, SB_BOTTOM, 0);
		}

		public void AppendLine(string str)
		{
			Append(str + "\r\n");
		}

		public void AppendFormat(string format, params object[] args)
		{
			Append(string.Format(format, args));
		}

		public void AppendFormatLine(string format, params object[] args)
		{
			AppendLine(string.Format(format, args));
		}

		public void ResetStyle()
		{
			FontStyle = FontStyle.Regular;
			Color = Color.Black;
			Bullet = false;
		}

	}
}
